This crate provides a fast streaming CSV (comma separated values) writer and
reader that works with the serialize
crate to do type based encoding
and decoding. There are two primary goals of this project:
- The default mode of parsing should just work. This means the parser will bias toward providing a parse over a correct parse (with respect to RFC 4180).
- Convenient to use by default, but when performance is needed, the API will provide an escape hatch.
There is evidence of this parser's performance at the bottom of this README. You can also see how it compares to other parsers in ewanhiggs' CSV game.
Dual-licensed under MIT or the UNLICENSE.
Documentation
The API is fully documented with lots of examples: http://burntsushi.net/rustdoc/csv/.
Simple examples
Here is a full working Rust program that decodes records from a CSV file. Each record consists of two strings and an integer (the edit distance between the strings):
extern crate csv;
Don't like tuples? That's fine. Use a struct instead:
extern crate csv;
extern crate rustc_serialize;
Do some records not have a distance for some reason? Use an Option
type!
You can also read CSV headers, change the delimiter, use enum
types or just
get plain access to records as vectors of strings. There are examples with more
details in the documentation.
Installation
This crate works with Cargo and is on
crates.io. The package is regularly updated.
Add it to your Cargo.toml
like so:
[]
= "0.14"
Performance and benchmarks
I claim that this is one of the fastest CSV parsers out there. Its speed should
be comparable or better than
libcsv
while providing a more convenient and safer interface. At the lowest level, the
parser can decode CSV at about 200 MB/sec. Here are some rough benchmarks:
raw ... bench: 5627467 ns/iter (+/- 171958) = 241 MB/s
byte ... bench: 9307428 ns/iter (+/- 473205) = 146 MB/s
string ... bench: 11043921 ns/iter (+/- 55845) = 122 MB/s
decoded ... bench: 16150376 ns/iter (+/- 496846) = 83 MB/s
raw
corresponds to the zero allocation parser. Namely, no allocations are
made for each field or row. For example, this is the fastest way to compute the
number of records in a CSV file:
extern crate csv;
use NextField;
byte
corresponds to allocating a fresh byte string for each field and a fresh
vector for each row. This is more convenient than using the raw
API:
extern crate csv;
string
is just like byte
, except each field is decoded from UTF-8 into a
Unicode string. It's exactly like above, except one uses records
instead of
byte_records
.
decoded
is the slowest approach but also the most convenient if your CSV
contains data other than plain strings, like numbers or booleans.
Indexing
This library also includes simplistic CSV indexing support. Once a CSV index is created, you can use it to jump to any record in the data instantly. In essence, it gives you random access for a modest upfront cost in time and memory.
This example shows how to create an in-memory index and use it to jump to
any record in the data. (The indexing interface works with seekable readers
and writers, so you can use std::fs::File
for this too.)
extern crate csv;
use ;
use ;